import wxauto 
import json
import time

print(wxauto.VERSION)

wx = wxauto.WeChat()

# msg = ''#'123'
# who = '长江'
# wx.SendMsg(msg=msg, who=who)

# wx.SendFiles(r'c:\Users\lucas\Desktop\实施方案240724批注.docx',who)


# who='Sweety\'s sweet family'
# at=['雯雯']
# wx.SendMsg(msg=msg, who=who,at=at)

# friends = wx.GetAllFriends()
# with open("friends.json",'w') as f:
#     json.dump(friends,f)
# for friend in friends:
#     print(friend)
    # print(f"好友昵称: {friend['nickname']}, 备注: {friend['remark']}")

# wx.LoadMoreMessage()

# msgs = wx.GetAllMessage()
# for msg in msgs:
#     if msg.type == 'sys':
#         print(f'【系统消息】{msg.content}')
    
#     elif msg.type == 'friend':
#         sender = msg.sender # 这里可以将msg.sender改为msg.sender_remark，获取备注名
#         print(f'{sender.rjust(20)}：{msg.content}')

#     elif msg.type == 'self':
#         print(f'{msg.sender.ljust(20)}：{msg.content}')
    
#     elif msg.type == 'time':
#         print(f'\n【时间消息】{msg.time}')

#     elif msg.type == 'recall':
#         print(f'【撤回消息】{msg.content}')

# msgs = wx.GetAllNewMessage()
# print(msgs)
# for msg in msgs:
#     if msg.type == 'sys':
#         print(f'【系统消息】{msg.content}')
    
#     elif msg.type == 'friend':
#         sender = msg.sender # 这里可以将msg.sender改为msg.sender_remark，获取备注名
#         print(f'{sender.rjust(20)}：{msg.content}')

#     elif msg.type == 'self':
#         print(f'{msg.sender.ljust(20)}：{msg.content}')
    
#     elif msg.type == 'time':
#         print(f'\n【时间消息】{msg.time}')

#     elif msg.type == 'recall':
#         print(f'【撤回消息】{msg.content}')

# msgs = wx.GetAllMessage()
# friend = 'Sweety'  # 昵称、备注、微信号都可以，手机号不可以
# msgs[-1].forward(friend)   # 转发最后一条消息给指定好友


listen_list=['雯雯']

for i in listen_list:
    wx.AddListenChat(who=i)


print("1")
time.sleep(20) #
while True:
    msgs = wx.GetListenMessage()
    for chat in msgs:
        one_msgs = msgs.get(chat)   # 获取消息内容
        
        # 回复收到
        for msg in one_msgs:
            if msg.type == 'sys':
                print(f'【系统消息】{msg.content}')
            
            elif msg.type == 'friend':
                sender = msg.sender # 这里可以将msg.sender改为msg.sender_remark，获取备注名
                print(f'{sender.rjust(20)}：{msg.content}')
                
                # ！！！ 回复收到，此处为`chat`而不是`wx` ！！！
                chat.SendMsg('收到')  
                # 此处将msg.content传递给大模型，再由大模型返回的消息回复即可实现ai聊天

            elif msg.type == 'self':
                print(f'{msg.sender.ljust(20)}：{msg.content}')
            
            elif msg.type == 'time':
                print(f'\n【时间消息】{msg.time}')

            elif msg.type == 'recall':
                print(f'【撤回消息】{msg.content}')

# sessions = wx.GetSession()

# for session in sessions:
#     print(f"============== 【{session.name}】 ==============")
#     print(f"最后一条消息时间: {session.time}")
#     print(f"最后一条消息内容: {session.content}")
#     print(f"是否有新消息: {session.isnew}")
